
<%@page import="java.net.URLEncoder"%>
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.DateUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%><%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@page import="com.spmanage.util.WebAction"%>
<%@page import="com.spmanage.util.StringUtil"%><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%
	WebAction webAction = new WebAction(request,response);
    System.out.println("山东以太-报告接收");
    
    String userPhone = webAction.getParameterString("phone");
    String linkId = webAction.getParameterString("linkid");
    String status = webAction.getParameterString("reportcode");
    String fee = webAction.getParameterString("feecode");
    String iod = webAction.getParameterString("iod");//上行指令，分配的精确的：596，不是实际的，所以没用
    String spNo = webAction.getParameterString("spnumber");
    String srvCode = webAction.getParameterString("svc");
    String date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());
    
    if(userPhone==null||linkId==null||status==null){
    	 response.getWriter().write("err");
    }else{
	    String org = SqlUtil.getStringResult("select org from "+BillingUtil.getSendDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
	    if(org!=null&&org.length()>0) {
	    	System.out.println("该linkid已经有同步状态");
	    	return;
	    }
	    
	    //通过下行linkid查询上行表中mo
	    String content = SqlUtil.getStringResult("select body from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
	    
	    //获取用户渠道号
	    int channelId = BillingUtil.getUserChannel(spNo,content);
	    int spid = BillingUtil.getUserSpId(spNo,content);
	    
	    System.out.println("山东以太端口:"+spNo+";指令："+content+";spid:"+spid+",channel:"+channelId+"");
	    //获取是否要扣量
		int ischeck = BillingUtil.getCheckSend(linkId);
	    int provinceId = BillingUtil.getProvinceId(userPhone);
	    System.out.println(provinceId);
	    StringBuilder sb = new StringBuilder();
	    sb.append("'");
	    sb.append(spid);
	    sb.append("',");
	    sb.append("1");//msgmode
	    sb.append(",");
	    sb.append("15");//fmt
	    sb.append(",");
	    sb.append("0");//futype
	    sb.append(",");
	    sb.append("2");//ftype
	    sb.append(",");
	    sb.append(fee);//feecode  
	    sb.append(",");
	    sb.append("'");
	    sb.append(srvCode);//stype
	    sb.append("',");
	    sb.append("1");//num
	    sb.append(",");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("'");
	    sb.append(spNo);
	    sb.append("',");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("getDate()");  //tm
	    sb.append(",");
	    sb.append("'");
	    sb.append("mid");  //mid
	    sb.append("',");
	    sb.append("'");
	    sb.append("msgid");  //msgid
	    sb.append("',");
	    sb.append("0");//result  DELIVRD
	    sb.append(",");
	    sb.append("'");
	    sb.append(status);//DELIVRD
	    sb.append("',");
	    sb.append("'");
	    sb.append(channelId);//channelId
	    sb.append("',");
	    sb.append("'");
	    sb.append(linkId);//linkid
	    sb.append("','"+content+"'");
	    sb.append(",");
	    sb.append(provinceId);
	    sb.append(",");
	    sb.append(ischeck);
	        
	    SqlUtil.executeUpdate("insert into "+BillingUtil.getSendDay()+"(areaid,msgMode,fmt,futype,ftype,feecode,stype,num,org,dest,fee,tm,mid,msgid,result,stat,infoid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
		
	    if(ischeck == 0) {//不扣量
	    	System.out.println("山东以太-状态同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channelId, 5)+"；渠道号："+channelId);
	    	String mtUrl = BillingUtil.getMtUrlByChannel(channelId);
    		if(mtUrl.indexOf("?")>0){
    			mtUrl = mtUrl+"&spNo="+spNo+"&userPhone="+userPhone+"&linkId="+linkId+"&fee="+fee+"&status="+status+"&date="+date+"&mo="+URLEncoder.encode(content,"UTF-8")+"";
    		}else{
    			mtUrl = mtUrl+"?spNo="+spNo+"&userPhone="+userPhone+"&linkId="+linkId+"&fee="+fee+"&status="+status+"&date="+date+"&mo="+URLEncoder.encode(content,"UTF-8")+"";
    		}
	    	
	     	System.out.println("山东以太-用户渠道号:"+channelId+";用户报告地址："+mtUrl);
	    	String str=HttpUtil.getHtmlSource(mtUrl,"UTF-8");
	    	System.out.println("山东以太-同步出去的地址:"+str);
	    }
	    response.getWriter().write("OK");
    }
  
%>
